package api

import (
	"RedWork/dao"
	"RedWork/util"
	"RedWork/util/errmsg"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"strconv"
	"time"
)

func AddOrder() gin.HandlerFunc {
	return func(c *gin.Context) {
		//uid := c.Query("user_id")
		//location := c.Query("location")
		status, _, uidint := util.HandleTokenCode(c)
		var ReqOrder dao.OrderReq
		var total float64
		err := c.BindJSON(&ReqOrder)
		fmt.Println(ReqOrder)
		if !status {
			return
		} else if uidint != ReqOrder.UserID {
			c.JSON(200, gin.H{
				"status": status,
				"data":   "请求用户id错误",
			})
			c.Abort()
			return
		}
		if err != nil {
			status = false
			c.JSON(200, gin.H{
				"status": status,
				"data":   errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT),
			})
			c.Abort()
			return
		}
		orderId, createTime, err := dao.CreateOrderId(ReqOrder.UserID, ReqOrder.Location)
		if err != nil {
			fmt.Println(err)
			status = false
		} else {
			total, err = dao.CreateOrder(ReqOrder, orderId)
			if err != nil {
				status = false
				fmt.Println(err)
				c.JSON(200, gin.H{
					"status": status,
					"data":   err.Error(),
				})
				return
			}
		}

		mapres := map[string]interface{}{
			"create_time":  createTime,
			"order_id":     orderId,
			"order_amount": total,
		}

		fmt.Println("order id is,", orderId)
		c.JSON(200, gin.H{
			"status": status,
			"data":   mapres,
		})
	}
}

func QueryOrder() gin.HandlerFunc { //查询用户订单信息
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			return
		}
		uid := c.Query("user_id")
		oid := c.Query("order_id")
		uidq, err := strconv.Atoi(uid)
		if err != nil {
			fmt.Println(err)
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
		}
		if uidq != uidint {
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
			c.JSON(200, gin.H{
				"status": false,
				"data":   data,
			})
			c.Abort()
			return
		}
		order := dao.QueryOrder(oid)
		dd, _ := time.ParseDuration("1h")
		//func (t Time) Before(u Time) bool
		//如果t代表的时间点在u之前，返回真；否则返回假。
		if order.CreateTime.After(time.Now().Add(dd)) && order.OrderStatus != 2 {
			order.OrderStatus = 0
		} else {
			fmt.Println("订单未过期")
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   order,
		})
	}
}

func CancelOrder() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			return
		}
		uid := c.Query("user_id")
		oid := c.Query("order_id")
		uidq, err := strconv.Atoi(uid)
		//oidint, err := strconv.Atoi(oid)
		if err != nil {
			fmt.Println(err)
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
		}
		if uidq != uidint {
			data = errmsg.GetErrMsg(errmsg.ERROR_TOKEN_WRONG)
			log.Println("uidq != uidint", data)
			c.JSON(200, gin.H{
				"status": false,
				"data":   data,
			})
			c.Abort()
			return
		}
		err = dao.CancelOrder(oid)
		if err != nil {
			fmt.Println(err)
			status = false
			data = err.Error()
		}

		c.JSON(200, gin.H{
			"status": status,
			"data":   data,
		})
	}
}

func PayOrder() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			return
		}
		uid := c.PostForm("user_id")
		oid := c.PostForm("order_id")
		if uid != strconv.Itoa(uidint) {
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
			c.JSON(200, gin.H{
				"status": false,
				"data":   data,
			})
			c.Abort()
			return
		}
		err := dao.PayOrder(uid, oid)
		if err != nil {
			status = false
			log.Println(err)
			data = err.Error()
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   data,
		})
	}
}

func QueryUserOrder() gin.HandlerFunc {
	return func(c *gin.Context) {
		status, data, uidint := util.HandleTokenCode(c)
		if !status {
			return
		}
		uid := c.Query("user_id")
		//oid := c.Query("order_id")
		code := c.Query("code")
		if uid != strconv.Itoa(uidint) {
			status = false
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
			c.JSON(200, gin.H{
				"status": false,
				"data":   data,
			})
			c.Abort()
			return
		}
		_, err := strconv.Atoi(code)
		if err != nil {
			log.Println(err)
			status = false
			data = errmsg.GetErrMsg(errmsg.PARM_NOT_RIGHT)
			c.JSON(200, gin.H{
				"status": false,
				"data":   data,
			})
			c.Abort()
			return
		}
		if code == "3" {
			code = ""
		}
		dd, _ := time.ParseDuration("1h")
		Orders := dao.QueryUserOrder(uid, code)
		for _, value := range Orders {
			if value.CreateTime.After(time.Now().Add(dd)) && value.OrderStatus != 2 {
				value.OrderStatus = 0
			} else {
				fmt.Println("订单未过期")
			}
			//value.CreateTime
		}
		c.JSON(200, gin.H{
			"status": status,
			"data":   Orders,
		})
	}
}
